#include "play.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include<QLabel>
//#include<QFont>
//#include<QPushButton>
#include"mypushbutton.h"
#include<QTimer>
#include"mycoin.h"
#include"dataconfig.h"
#include<iostream>
#include<QPropertyAnimation>
#include<QSound>

using namespace std;
//play::play(QWidget *parent) : QMainWindow(parent)
//{

//}

play::play(int levelNum)
{



    //qDebug()<<"x= "<<coinBtn[1][1]->posX<<"  y= "<<coinBtn[1][1]->posY;
    mycoin*  coin;

    dataConfig config;
      for(int i = 0 ; i < 4;i++)
      {
          for(int j = 0 ; j < 4; j++)
          {
              gameArray[j][i] = config.mData[levelNum][i][j];
          }
      }




    qDebug()<<QString("进入第 %1 关").arg(levelNum);
    this->levelIndex = levelNum;
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("翻金币场景");

    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    QMenu * start = bar->addMenu("开始");
    QAction * quit = start->addAction("quit");
    connect(quit,&QAction::triggered,[=](){
        this->close();
    });




    QLabel* winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5 , -tmpPix.height());





    MypushButton *backBtn = new MypushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MypushButton::clicked,[=]()
    {
        qDebug()<<"点击了返回";

        QTimer::singleShot(200,this,[=](){
        emit this->Choosebackbutton();
        });
    });
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(20);
    QLabel * label = new QLabel;
    label->setParent(this);
    QString str = QString("Level:%1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(str);
    label->setGeometry(30,this->height()-50,120,50);

    //创建金币的背景图片
       for(int i = 0 ; i < 4;i++)
       {
           for(int j = 0 ; j < 4; j++)
           {
              //绘制背景图片
               QLabel* label = new QLabel;
               label->setGeometry(0,0,50,50);
               label->setPixmap(QPixmap(":/res/BoardNode.png"));
               label->setParent(this);
               label->move(57 + i*50,200+j*50);

               QString img;
           if(gameArray[i][j] == 1)
           {
             img = ":/res/Coin0001.png";
           }
           else if(gameArray[i][j] == 0)
           {
             img = ":/res/Coin0008.png";
           }
           coin = new mycoin(img);
           coin->setParent(this);
           coin->move(59 + i*50,204+j*50);
           coin->posX = i; //记录x坐标
           coin->posY = j; //记录y坐标
           //qDebug()<<"x= "<<coin->posX<<"  y= "<<coin->posY;
           coin->flag =gameArray[i][j]; //记录正反标志
           coinBtn[i][j] = coin;

           //coinBtn[i][j]->changeFlag();
//           mycoin* coin = new mycoin(":/res/Coin0001.png");
//           coin->setParent(this);
//           coin->move(59 + i*50,204+j*50);

           connect(coin,&mycoin::clicked,[=](){
                   //qDebug() << "点击的位置： x = " <<  coin->posX << " y = " << coin->posY ;
                   coin->changeFlag();
      gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0; //数组内部记录的标志同步修改
                   //qDebug()<<coinBtn[3][1]->posX;




                   QTimer::singleShot(300, this,[=](){
                       if(coin->posX+1 <=3)
                       {
                         coinBtn[coin->posX+1][coin->posY]->changeFlag();
                         gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                         //qDebug()<< gameArray[coin->posX+1][coin->posY];
                       }
                       if(coin->posX-1>=0)
                       {
                         coinBtn[coin->posX-1][coin->posY]->changeFlag();
                         gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;
                       }
                       if(coin->posY+1<=3)
                       {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]== 0 ? 1 : 0;
                       }
                       if(coin->posY-1>=0)
                       {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]== 0 ? 1 : 0;
                       }

                       for(int i = 0 ; i < 4;i++)
                       {
                           for(int j = 0 ; j < 4; j++)
                           {
                               cout<<gameArray[j][i]<<" ";
                           }
                               cout<<endl;
                       }
                       for(int i = 0 ; i < 4;i++)
                       {
                           for(int j = 0 ; j < 4; j++)
                           {
                               isWin+=gameArray[i][j];
                           }
                       }
                       qDebug()<<isWin;
                       if(isWin ==16)
                       {
                           QPropertyAnimation *animatjion = new QPropertyAnimation(winLabel,"geometry");
                           animatjion->setDuration(1000);
                           //start position
                           animatjion->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                           animatjion->setEndValue(QRect(winLabel->x(),winLabel->y()+144,winLabel->width(),winLabel->height()));
                           animatjion->setEasingCurve(QEasingCurve::OutBounce);
                           animatjion->start();
                           qDebug()<<"游戏胜利啦";
                           for(int i = 0 ; i < 4;i++)
                           {
                               for(int j = 0 ; j < 4; j++)
                               {
                                   coinBtn[i][j]->isWin = true;
                               }
                           }
                       }
                       isWin = 0;

                      });
                    });
//                   for(int i = 0 ; i < 4;i++)
//                   {
//                       for(int j = 0 ; j < 4; j++)
//                       {
//                           qDebug()<<gameArray[j][i];
//                       }
//                           qDebug()<<endl;
//                   }

                   //判断是否胜利





           }
       }




}

void play::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}




//QMenuBar * bar = menuBar();
//setMenuBar(bar);

//QMenu * startMenu = bar->addMenu("开始");
//QAction * quitAction = startMenu->addAction("退出");
//connect(quitAction,&QAction::triggered,[=](){
//    this->close();
//});
